<template>
  <div class="createPay">
    <div>
      <jc-button
        :btn-arr="['reload', 'save', 'sub', 'remove', 'pass', 'list']"
        :disab-arr="[isDisabled && 'save', 'close', 'cancel', 'copy', 'com1', 'com2', 'using', 'freeze']"
        :list="'/statementList'"
        @refresh="transferDetails"
        @save="newlyAdjustment"
        @submit="newlyAdjustment(1)"
        @noSubmit="submit(2)"
        @audit="audit(1)"
        @noAudit="audit(2)"
        @del="remove"
      />
    </div>
    <el-tabs v-model="activeName">
      <el-tab-pane :label="$t('market.info')" name="first">
        <el-form :model="subDetails" label-width="100px" size="mini">
          <el-form-item :label="$t('market.state')">
            <el-tag :type="subDetails.stateColor">{{ subDetails.fdocumentstatus }}</el-tag>
          </el-form-item>
          <el-form-item :label="$t('market.billNo')">
            <el-input v-model="subDetails.fbillno" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.billType')">
            <el-input v-model="subDetails.fbilltypname" disabled />
          </el-form-item>
          <!--          <el-form-item label="创建组织">-->
          <!--            <el-input v-model.trim="subDetails.fcreateorgname" disabled />-->
          <!--          </el-form-item>-->
          <el-form-item :label="$t('market.bank')">
            <el-select v-model.trim="subDetails.fbankid" filterable :disabled="isDisabled">
              <el-option
                v-for="option in account1"
                :key="option.fid"
                :label="option.fname"
                :value="option.fid"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('market.bankcode')">
            <el-input v-model="subDetails.fbankaccount" :disabled="isDisabled">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="bankShow('', 2)"
              />
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('market.accountname1')">
            <el-input v-model.trim="subDetails.fbankaccountname" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.currency2')">
            <el-input v-model="subDetails.fcurrencyname" :disabled="isDisabled">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="openCurrency=true"
              />
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('market.onsetDate')">
            <el-date-picker
              v-model.trim="subDetails.fdate"
              type="date"
              value-format="yyyy-MM-dd"
              :disabled="isDisabled"
            />
          </el-form-item>
          <el-form-item :label="$t('market.endData')">
            <el-date-picker
              v-model.trim="subDetails.fenddate"
              type="date"
              value-format="yyyy-MM-dd"
              :disabled="isDisabled"
            />
          </el-form-item>
          <el-form-item :label="$t('market.firstbalance')">
            <el-input v-model.trim="subDetails.ffirstbalance" :disabled="isDisabled" />
          </el-form-item>
          <el-form-item :label="$t('market.finalbalance')">
            <el-input v-model.trim="subDetails.ffinalbalance" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.totaldebitamount')">
            <el-input v-model.trim="subDetails.ftotaldebitamount" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.totalcreditamount')">
            <el-input v-model.trim="subDetails.ftotalcreditamount" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.debitnum')">
            <el-input v-model.trim="subDetails.fdebitnum" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.creditnum')">
            <el-input v-model.trim="subDetails.fcreditnum" disabled />
          </el-form-item>
        </el-form>
        <div class="table-content">
          <div style="margin-bottom: 5px;">
            <el-button size="mini" @click="newHang">{{ $t('btn.newHang') }}<!--新增行--></el-button>
            <el-button size="mini" @click="delHang">{{ $t('btn.delHang') }}<!--删除行--></el-button>
            <el-button size="mini" @click="batchFill">{{ $t('btn.batchFill') }}<!--批量填充--></el-button>
          </div>
          <div>
            <jc-table
              :table-data="tableData"
              :table-header="tableHeader"
              :table-height="tableHeight"
              serial
              @rowClick="rowClick"
              @cellClick="cellClick"
            >
              <el-table-column :label="$t('market.businessDate')" align="center" width="120">
                <template slot-scope="scope">
                  <el-date-picker
                    v-model.trim="scope.row.fentrydate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    size="mini"
                    :disabled="isDisabled"
                  />
                </template>
              </el-table-column>
              <el-table-column :label="$t('market.amountIn')" align="center" width="80px">
                <template slot-scope="scope">
                  <el-input-number v-model="scope.row.fdebitamount" :min="0" size="mini" :disabled="isDisabled" />
                </template>
              </el-table-column>
              <el-table-column :label="$t('market.amountOut')" align="center" width="80px">
                <template slot-scope="scope">
                  <el-input-number v-model="scope.row.fcreditamount" :min="0" size="mini" :disabled="isDisabled" />
                </template>
              </el-table-column>
              <el-table-column :label="$t('market.money')" align="center" width="80px">
                <template slot-scope="scope">
                  <el-input-number v-model="scope.row.fbalance" disabled size="mini" />
                </template>
              </el-table-column>
              <el-table-column :label="$t('market.digest')" align="center" width="150">
                <template slot-scope="scope">
                  <el-input v-model.number="scope.row.fabstract" size="mini" :disabled="isDisabled" />
                </template>
              </el-table-column>
              <el-table-column :label="$t('market.settleType')" align="center" width="150px" prop="fsettleid">
                <template slot-scope="scope">
                  <el-select v-model.trim="scope.row.fsettleid" filterable :disabled="isDisabled">
                    <el-option
                      v-for="option in account2"
                      :key="option.fid"
                      :label="option.fname"
                      :value="option.fid"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column :label="$t('market.settlementnum')" align="center" width="150">
                <template slot-scope="scope">
                  <el-input v-model.number="scope.row.fnum" size="mini" :disabled="isDisabled" />
                </template>
              </el-table-column>
              <el-table-column :label="$t('market.othersbankname')" align="center" width="150px" prop="fbanknameid">
                <template slot-scope="scope">
                  <el-select v-model.trim="scope.row.fothersbankname" filterable :disabled="isDisabled">
                    <el-option
                      v-for="option in account1"
                      :key="option.fid"
                      :label="option.fname"
                      :value="option.fid"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column :label="$t('market.othersbankaccount')" align="center" width="170px">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.fothersbankaccount" :disabled="isDisabled">
                    <i
                      slot="suffix"
                      class="el-input__icon el-icon-search"
                      @click="bankShow(scope.$index, 1)"
                    />
                  </el-input>
                </template>
              </el-table-column>
              <el-table-column :label="$t('market.othersaccountname')" align="center" width="150px" prop="fothersaccountname" />
            </jc-table>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.other')" name="log">
        <jc-marker
          ref="log"
          :fnumber="subDetails.fbillno"
          other-height="65vh"
          :other-url-object="otherUrlObject"
        />
      </el-tab-pane>
    </el-tabs>
    <!--币别-->
    <currency v-if="openCurrency" @currency="currencySelectRow" />
    <!--银行账号-->
    <bank v-if="bankVisiblit" @client="bankData" />
  </div>
</template>

<script>
import { queryViewItem } from '@/api/marketManage/marketOrder'
import autocomplete from '@/views/market/marketManage/createMarkerOrder/components/autocomplete'
import bility from '@/mixin/Operability'
import {
  delBankChecks,
  notsubmitBankChecks, queryBankCheckDetails,
  reviewBankChecks, notreviewBankChecks,
  submitBankChecks, updateBankCheck
} from '@/api/financial/teller'
import { notoMxBmina, toMxBmina } from '@/components/ToMxamineState'
import jcButton from '@/components/Button'
import jcMarker from '@/components/marker'

export default {
  name: 'StatementAdd',
  components: {
    jcButton,
    jcMarker,
    bank: () => import('@/views/financial/cashier/payment/component/bank'),
    currency: () => import('@/views/market/marketManage/createMarkerOrder/components/currency')
  },
  mixins: [autocomplete, bility],
  data() {
    return {
      account1: [], // 银行
      account2: [], // 结算方式
      bankVisiblit: false, // 银行账号
      openCurrency: false, // 币别、价目表、价格类型
      subDetails: {},
      otherUrlObject: {},
      activeName: 'first',
      billtypes: [], // 单据类型
      indexSelf: 0,
      isDisabled: false,
      // 表头
      tableData: [], // 银行转账单数据
      tableHeight: '70vh',
      tableHeader: []
    }
  },
  created() {
    this.queryTBasBilltype1()
    this.getForm()
    if (this.$route.params.id) {
      this.transferDetails()
    }
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 320px)' : '73vh'
  },
  methods: {
    transferDetails() {
      const fid = this.$route.params.id
      queryBankCheckDetails({ fid }).then(res => {
        if (res.code === 0) {
          this.otherUrlObject = res.data
          this.subDetails = toMxBmina(res.data)
          this.tableData = res.data.details
          this.tableData.map(res => {
            res.fothersbankname = Number(res.fothersbankname)
          })
          this.isDisabled = this.subDetails.fdocumentstatus === this.$t('market.state2') || this.subDetails.fdocumentstatus === this.$t('market.state1')
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 保存
    newlyAdjustment(ev) {
      const DATA = {
        ...notoMxBmina(this.subDetails),
        details: this.tableData
      }
      updateBankCheck(DATA).then(res => {
        if (res.code === 0) {
          ev === 1 ? this.submit(1) : this.transferDetails()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 开户银行数据
    async getForm() {
      await queryViewItem({ viewName: 'SX_FIN_BANK' }).then(res => {
        this.account1 = res.data.list
      })
    },
    // 查询结算方式
    async queryTBasBilltype1() {
      const { data: RES } = await queryViewItem({ viewName: 'SX_SETTLETYPE' })
      this.account2 = RES.list
    },
    // 银行弹窗
    bankShow(index, ev) {
      this.outIn = ev
      this.indexSelf = index
      this.bankVisiblit = true
    },
    bankData(ev) {
      if (ev.fbankaccountId && this.outIn === 1) { // 表格
        const item = this.tableData[this.indexSelf]
        item.fothersbankaccountid = ev.fbankaccountId
        item.fothersbankaccount = ev.faccount
        item.fothersaccountname = ev.faccountname
      } else if (ev.fbankaccountId && this.outIn === 2) { // 表头
        this.subDetails.fbankaccountid = ev.fbankaccountId
        this.subDetails.fbankaccount = ev.faccount
        this.subDetails.fbankaccountname = ev.faccountname
      }
      this.bankVisiblit = false
    },
    // 币别选中
    currencySelectRow(ev) {
      const item = this.subDetails
      if (ev.fsettleCurrId) { // 转入
        item.fcurrencyid = ev.fsettleCurrId
        item.fcurrencyname = ev.fsettleCurrIdName
      }
      this.openCurrency = false
    },
    // 单击行
    rowClick(row) {
      this.indexSelf = row.index
    },
    // 新增行
    newHang() {
      this.tableData.push({
        fentrydate: this.subDetails.fdate,
        fdebitamount: 0, fcreditamount: 0,
        fbalance: '', fsettleid: 91,
        fabstract: '', fnum: '',
        fothersbankname: '', fothersbankaccount: ''
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: [this.subDetails.fbillno] }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.transferDetails()
        this.selectVal = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    submit(ev) {
      const api = ev === 1 ? submitBankChecks : notsubmitBankChecks
      this.toExamineFun(api)
    },
    // 审核
    audit(ev) {
      const api = ev === 1 ? reviewBankChecks : notreviewBankChecks
      this.toExamineFun(api)
    },
    // 删除
    remove() {
      this.$confirm('确定要删除该订单么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const api = delBankChecks
        const DATA = { Numbers: [this.subDetails.fbillno] }
        api(DATA).then(res => {
          if (res.code !== 0) {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
            return
          }
          this.innerHTM = ''
          res.data.forEach(res => {
            this.innerHTM += '<div>' + res + '</div>'
          })
          this.$notify.warning({
            title: '提示',
            message: this.innerHTM,
            duration: 3000, offset: 50,
            dangerouslyUseHTMLString: true
          })
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          this.$router.push({ path: '/statementList' })
        })
      })
    },
    // 删除行
    delHang() {
      this.tableData.splice(this.indexSelf, 1)
    },
    // 点击单元格
    cellClick(row, column, cell, event) {
      this.property = column.property
      this.subscript = row.index
    },
    // 批量填充
    batchFill() {
      const item = this.tableData[this.subscript][this.property]
      this.tableData.map((res, index) => {
        if (index >= this.subscript) {
          res[this.property] = item
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.tabPosit {
  padding: 5px;
  box-shadow: 0 0 2px #aaa;
  position: absolute;
  top: 3.5vh;
  left: 0;
  background-color: rgba(255, 255, 255, 1);
  z-index: 99999!important;
}
.tabPosit1 {
  padding: 5px;
  box-shadow: 0 0 2px #aaa;
  position: absolute;
  top: -25.69vh;
  left:0;
  background-color: rgba(255, 255, 255, 1);
  z-index: 9999;
}
.createPay {
  @include listBom;
  .header{
    margin-top: 10px;
    .header-name{
      span {
        width: 100px;
        margin-right: 10px;
        text-align: right;
      }
    }
  }
  .el-table{
    &::v-deep .el-input-number .el-input__inner{
      padding: 0;
      width: 75px;
    }
    &::v-deep .el-input-number .el-input{
      width: 75px;
    }
    &::v-deep .el-input-number--mini{
      width: 75px
    }
    &::v-deep .el-input-number__increase{
      display: none;
    }
    &::v-deep .el-input-number__decrease{
      display: none;
    }
    &::v-deep .el-table__footer-wrapper tbody td{
      background: #fdf6ec!important;
    }
  }
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;

    .el-form-item {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      height: 35px;
      width: 268px;
      margin-bottom: 2px;
      .btnType {
        margin-right: 20px;
      }

      .el-date-editor.el-input {
        width: 100% !important;
      }

      .el-select {
        width: 100%;
      }
    }
  }
}
</style>
